package com.ewiqi.springsecuritydemo.service;

import com.ewiqi.springsecuritydemo.dao.PermissionDao;
import com.ewiqi.springsecuritydemo.dao.RoleDao;
import com.ewiqi.springsecuritydemo.dao.UserDao;
import com.ewiqi.springsecuritydemo.model.Permission;
import com.ewiqi.springsecuritydemo.model.Role;
import com.ewiqi.springsecuritydemo.model.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author fengyu.zhang
 * @date 2021/1/19 11:58 上午
 */
@Service
public class SecurityServiceImpl implements SecurityService {
    @Resource
    UserDao userDao;

    @Resource
    PermissionDao permissionDao;

    @Resource
    RoleDao roleDao;

    @Override
    public int createPermission(Permission permission) {
        return permissionDao.insert(permission);
    }

    @Override
    public void updatePermission(Permission permission) {
        permissionDao.update(permission);
    }

    @Override
    public void deletePermission(int id) {
        permissionDao.delete(id);
    }

    @Override
    public int createRole(Role role) {
        return roleDao.insert(role);
    }

    @Override
    public void updateRole(Role role) {
        roleDao.update(role);
    }

    @Override
    public void deleteRole(int id) {
        roleDao.delete(id);
    }

    @Override
    public int createUser(User user) {
        return userDao.insert(user);
    }

    @Override
    public void updateUser(User user) {
        userDao.update(user);
    }

    @Override
    public void deleteUser(int id) {
        userDao.delete(id);
    }

    @Override
    public User loadUser(int id) {
        return userDao.findById(id);
    }

    @Override
    public User findByUsername(String username) {
        return userDao.findWithRoleAndPermissionByUsername(username);
    }

    @Override
    public List<User> loadUser() {
        return userDao.load();
    }

    @Override
    public Permission loadPermission(int id) {
        return permissionDao.findById(id);
    }

    @Override
    public List<Permission> loadPermission() {
        return permissionDao.load();
    }

    @Override
    public Role loadRole(int id) {
        return roleDao.findById(id);
    }

    @Override
    public List<Role> loadRole() {
        return roleDao.load();
    }
}
